<?php
require_once "funct.rss.php";
require_once "funct.testaHtml.php";

function getHtmlRigaRif($n=20) {
	$w = 100/$n;
	$sHtml = "<tr>";
	for($k=0; $k<$n; $k++) {
		$sHtml .= "<td style='width:$w%;padding:0;font-size:1pt;'>&nbsp;</td>";
	}
	$sHtml .= "</tr>\n";
	return $sHtml;
}

/**
 * genera campi form da array aCamForm
 * aCamForm riga=>cella=>aParCella
 * 	aParCella = array(nome,etichetta,slot etich.,slot campo,tipo,lungh.,lungh.max,nome var html)
 * 		nome var html = nome variabile stringa che contiene html
 * sModo = 	inp -> campi modificabili
 * 			hid -> campi hidden + contenuto visualizzato
 * 			vis -> solo contenuto visualizzato
 */
function getHtmlCampiForm($aCamForm, $sModo='inp',$aVarExt=array()) {
	//dbg_($aVarExt);
	$sHtml = "";
	foreach($aCamForm as $rX => $aRX) {
	   $sHtml .= "\t<tr>\n";
		foreach($aRX as $cc => $aCX) {
			$tipoCampo = $aCX[4];
			if (!isset($aCX[7]) || !is_array($aCX[7])) { $aCX[7] = array();}
			if ($tipoCampo == 'hidden') {
				$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
				continue;
			}
			if ($aCX[2]>0) {
		      $sHtml .= "\t\t<td class='lab1' colspan='".$aCX[2]."' valign='top'>"._t($aCX[1])."</td>\n";
			}
			if ($aCX[3]>0) {
				$sHtml .= "\t\t<td class='txt1' colspan='".$aCX[3]."'>\n";
				if ($sModo == 'vis' or $sModo == 'hid') {
					switch($tipoCampo) {
						case 'textnum' :
							$tipoCampo = 'stringeuro';
							break;
						case 'textarea' :
						case 'selectm' :
							$tipoCampo = 'note';
							break;
						case 'password' :
							$tipoCampo = 'passwordx';
							break;
						case 'select' :
						case 'radio' :
						case 'radiov' :
						case 'decode' :
							$tipoCampo = 'decode';
							break;
						default : 
							$tipoCampo = 'string';
							break;
					} 
				}
				if ($aCX['0'] != "") {
					switch($tipoCampo) {
						case 'radio' :
							if (!isset($aCX[6]) || !is_array($aCX[6])) { $aCX[6] = array();}
							$sHtml .= "\t\t\t".getHtmlRadio($aCX[0],$aCX[5],'@REQ',$aCX[7]);
							break;
						case 'radiov' :
							if (!isset($aCX[6]) || !is_array($aCX[6])) { $aCX[6] = array();}
							$aCX[6]['aCapo'] = true;
							$sHtml .= "\t\t\t".getHtmlRadio($aCX[0],$aCX[5],'@REQ',$aCX[7]);
							break;
						case 'select' :
							if (!isset($aCX[6]) || !is_array($aCX[6])) { $aCX[6] = array();}
							$sHtml .= "\t\t\t".getHtmlSelect($aCX[0],$aCX[5],'@REQ',$aCX[7]);
							break;
						case 'selectm' :
							if (!isset($aCX[6]) || !is_array($aCX[6])) { $aCX[6] = array();}
							$sHtml .= "\t\t\t".getHtmlSelectMult($aCX[0],$aCX[5],'@REQ',$aCX[7]);
							break;
						case 'checkbox' :
							if (!isset($aCX[6]) || !is_array($aCX[6])) { $aCX[6] = array();}
							$sHtml .= "\t\t\t".getHtmlCheckBox($aCX[0],"ok",'@REQ',$aCX[7]);
							break;
						case 'textcode' :
							$sHtml .= "\t\t\t".getHtmlTextcode($aCX[0],$aCX[5],$aCX[6],'@REQ',$aCX[7]);
							break;
						case 'textnum' :
							$sHtml .= "\t\t\t".getHtmlTextNum($aCX[0],$aCX[5],$aCX[6],'@REQ',$aCX[7]);
							break;
						case 'date' :
							$sHtml .= "\t\t\t".getHtmlDate($aCX[0])."\n";
							break;
						case 'textarea' :
							$sHtml .= "\t\t\t".getHtmlTextarea($aCX[0],$aCX[5],$aCX[6],'@REQ',$aCX[7]);
							break;
						case 'string' :
						case 'stringnum' :
						case 'stringeuro' :
							$sValVis = _testLink(reqVar($aCX[0]),$aCX[7],$aVarExt);
							$sHtml .= "\t\t\t".$sValVis."\n";
							if ($sModo == 'hid') {
								$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
							}
							break;
						case 'passwordx' :
							$sHtml .= "\t\t\t*********\n";
							if ($sModo == 'hid') {
								$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
							}
							break;
						case 'decode' :
							$sValVis = (isset($aCX[5][reqVar($aCX[0])]))?$aCX[5][reqVar($aCX[0])]:reqVar($aCX[0]);
							$sValVis = _testLink($sValVis,$aCX[7],$aVarExt);
							$sHtml .= "\t\t\t$sValVis&nbsp;\n";
							if ($sModo == 'hid') {
								$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
							}
							break;
						case 'note' :
							$sHtml .= "\t\t\t".getHtmlViewNote(reqVar($aCX[0]),$aCX[7])."\n";
							if ($sModo == 'hid') {
								$sHtml .= "\t\t\t".getHtmlHidden($aCX[0]);
							}
							break;
						case 'password' :
							$sHtml .= "\t\t\t".getHtmlPassword($aCX[0],$aCX[5],$aCX[6])."\n";
							break;
						case 'vuoto' :
							break;
						case 'text' :
						default:
							$sHtml .= "\t\t\t".getHtmlText($aCX[0],$aCX[5],$aCX[6])."\n";
							break;
					}
				}
				//dbg_($aCX);
				if (isset($aCX[7]['varExt']) && isset($aVarExt[$aCX[7]['varExt']])) {
					$sHtml .= $aVarExt[$aCX[7]['varExt']]."\n";
				}
				$sHtml .= "\t\t</td>\n";
			}
		}
	   $sHtml .= "\t</tr>\n";
	}
	return $sHtml;
}
function _testLink($str,$aOpt,$aVarExt) {
	if (!isset($aOpt['href']) || !isset($aVarExt[$aOpt['href']])) {
		return $str;
	}
	return "<a href=\"".$aVarExt[$aOpt['href']]."\" class=\"txt1\">$str</a>";
}

function getHtmlForm($name='', $action='', $aOpt=array()) {
	$sEncType = ' enctype="multipart/form-data"';//(isset($aOpt['enctype']))?' enctype="multipart/form-data"':'';
	if ($action == '') {
		$action = $_SERVER['PHP_SELF'];
	}
	if ($name == '') {
		$name = 'frm1';
	}
	$sTarget = '';
	if (isset($aOpt['target'])) {
		$sTarget = "target='".$aOpt['target']."'";
	}
	$sHtml = '<form name="'.$name.'" action="'.$action.'" method="post"'
		.$sEncType." style='margin:0;padding:0;' $sTarget>\n";
	return $sHtml;
}

function getHtmlTextCode($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	if (!isset($aOpt['style'])) {
		$aOpt['style'] = "";
	}
	$aOpt['style'] .= 'font-family:lucida console,courier;';
	$sHtml = getHtmlText($name, $size, $maxLen, $val, $aOpt) ;
	return $sHtml;
}

function getHtmlTextNum($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	if (!isset($aOpt['style'])) {
		$aOpt['style'] = "";
	}
	$aOpt['style'] .= 'text-align:right;';
	return getHtmlText($name, $size, $maxLen, $val, $aOpt);
} 

function getHtmlText($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'" ':'';
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sMaxLen = ($maxLen==0)?'':' maxlength="'.$maxLen.'"';
	$sStyle = (isset($aOpt['style']))?' style="'.$aOpt['style'].'"':'';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sSize = ' size="'.($size*1).'"';
	$sHtml = '<input type="text" class="inp1" name="'.$name.'" id="'.$name.'" value="'.$sVal.'" '
		.$sSize.$sMaxLen.$sReadOnly.$sStyle.$sOnChange.">\n";
	return $sHtml;
}

function getHtmlDate($name, $val='@REQ', $nomeForm='frm1', $aOpt=array()) {
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'" ':'';
	$sOpzSubm = ($sOnChange=='')?"":"submOrig=S&";
	$sMaxLen = '10';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = "<input type=\"text\" class=\"inp1\" name=\"$name\" id=\"$name\" value=\"$sVal\"".
		" size=\"8\" maxlength=\"10\" $sReadOnly $sOnChange>".
		"&nbsp;<img src='".RGL_URL_ROOT."/app/_img/calendario.png' " .
		"style='text-decoration:underline;cursor:pointer;'  onClick=\"apriCalend_$nomeForm$name();\" />\n";
	$sHtml .= "<script>";
	$sHtml .= "function apriCalend_$nomeForm$name() {\n" .
		"dtVal = encodeURIComponent(document.$nomeForm.$name.value);\n".
		"window.open('".RGL_URL_ROOT."/app/_com/calendario.php?$sOpzSubm" .
		"nome_form=$nomeForm&nome_campo=$name&dt='+dtVal+'&xsn=".XSN."'" .
		",'Calendario','top=200,left=100,width=290,height=240," .
		"resizable=yes,titlebar=no,location=no,location=no,menubar=no,toolbar=no');\n".		
			"}\n";
	$sHtml .= "</script>\n";
	
	return $sHtml;
}

function getHtmlPassword($name, $size, $maxLen=0, $val='@REQ', $aOpt=array()) {
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sMaxLen = ($maxLen==0)?'':' maxlength="'.$maxLen.'"';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<input type="password" class="inp1" name="'.$name.'" id="'.$name.'" ' .
			' value="'.$sVal.'" size="'.$size.'"'.
		$sMaxLen.$sReadOnly.' style="font-family:lucida console,courier">'."\n";
	return $sHtml;
}

function getHtmlFile($name, $size, $aOpt=array()) {
	$sHtml = '<input type="file" class="inp1" name="'.$name.'" id="'.$name.'" size="'.$size."\">\n";
	return $sHtml;
}

function getHtmlTextArea($name, $cols=80, $rows=5, $val='@REQ', $aOpt=array()) {
	$sReadOnly = (isset($aOpt['readonly']))?' READONLY':'';
	$sClass = (isset($aOpt['class']))?$aOpt['class']:'inp1';
	$sStyle = (isset($aOpt['style']))?' style="'.$aOpt['style'].'"':'';
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<textarea class="'.$sClass.'" name="'.$name.'" id="'.$name.'" ' 
		.'cols="'.$cols.'" rows="'.$rows.'"'.$sReadOnly.$sStyle.'>'
		.$sVal.'</textarea>'."\n";
	return $sHtml;
}

function getHtmlViewText($txt) {
	return htmlentities($txt);
}

function getHtmlViewCode($txt) {
	$sHtml = "<span style=\"font-family:lucida console,courier\">".
	getHtmlViewText($txt)."</span>";
	return $sHtml;
}

function getHtmlViewNote($txt,$aOpt=array()) {
	$sStyle = (isset($aOpt['style']))?' style="'.$aOpt['style'].'"':'';
	$sHtml = "<span$sStyle>";
	$sHtml .= nl2br(str_replace("\t","&nbsp;&nbsp;&nbsp;",
		str_replace('  ',' &nbsp;',htmlentities($txt))))."</span>";
	return $sHtml;
}

function getHtmlViewDate($sAMG,$formatoOut='d/m/Y') {
	return date($formatoOut,strtotime($sAMG));
}

function getHtmlViewNum($num,$nDec,$leftZero=0) {
	return num2str($num,$nDec,$leftZero);
}

function getHtmlVisText($nome,$aOpt=array()) {
	return htmlentities(reqvar($nome));
}

function getHtmlVisCode($nome) {
	$sHtml = "<span style=\"font-family:lucida console,courier\">".
	getHtmlVisText($nome)."</span>";
	return $sHtml;
}

function getHtmlVisNote($nome,$aOpt=array()) {
	return getHtmlViewNote(reqvar($nome),$aOpt);
}

function getHtmlVisNum($nome,$nDec,$leftZero=0) {
	return num2str(reqVar($nome),$nDec,$leftZero);
}

function getHtmlVisDecode($nome,$aVal) {
	return (isset($aVal[reqVar($nome)]))?$aVal[reqVar($nome)]:reqVar($nome);
}

function getHtmlRadio($name, $aVal, $val='@REQ', $aOpt=array()) {
	$sVal = ($val=='@REQ')? reqVar($name):$val;
	$sHtml='';
	$sSep = (isset($aOpt['aCapo']) and $aOpt['aCapo'])?"<br>\n":"&nbsp;&nbsp;&nbsp;\n";
	$sOnClick = (isset($aOpt['onClick']) and $aOpt['onClick'])?" onClick=\"".$aOpt['onClick']."\"":"";
	$i = 1;
	$sSepX = '';
	foreach($aVal as $vX => $dX) {
		$chk = ($sVal == $vX)?' checked':'';
		$sDes = ($dX=='')?'':'&nbsp;'.htmlentities($dX);
		$sHtml .= $sSepX.'<input type="radio" id="'.$name."_".$i.'" name="'.$name.'" value="'.htmlentities($vX).'"'.$chk.$sOnClick.
			' class="inp1">'.$sDes;
		$sSepX = $sSep;
		$i ++;
	}
	return $sHtml;
}

function getHtmlCheckBox($name, $valChecked, $val='@REQ', $aOpt=array()) {
	$sVal = ($val=='@REQ')? reqVar($name):$val;
	$sOnClick = (isset($aOpt['onClick']) and $aOpt['onClick'])?" onClick=\"".$aOpt['onClick']."\"":"";
	$sHtml='';
	$chk = ($sVal == $valChecked)?' checked':'';
	$sHtml .= '<input type="checkBox" id="'.$name.'" name="'.$name.'" ' .
			'value="'.htmlentities($valChecked).'" '.$chk.' class="inp1" '.$sOnClick.">\n";
	return $sHtml;
}

function getHtmlSelect($name, $aVal, $val='@REQ', $aOpt=array()) {
	$sVal = ($val=='@REQ')? reqVar($name):$val;
	$sDisabled = (isset($aOpt['disabled']))?' disabled':'';
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'"':'';
	$style = (isset($aOpt['style']))?' style="'.$aOpt['style'].'"':'';
	$sHtml= "<select class=\"inp1\" id=\"$name\" name=\"$name\" $sOnChange $style $sDisabled>";
	foreach($aVal as $vX => $dX) {
		$sel = ($sVal == $vX)?' selected':'';
		$sHtml .= "<option value=\"".htmlentities($vX)."\"$sel>$dX</option>";
	}
	$sHtml.= "</select>\n";
	return $sHtml;
}

function getHtmlSelectMult($name, $aVal, $val='@REQ', $aOpt=array()) {
	$sVal = ($val=='@REQ')? reqVar($name):$val;
	$sOnChange = (isset($aOpt['onChange']))?' onChange="'.$aOpt['onChange'].'"':'';
	$sHtml='<select class="inp1" id="'.$name.'_cmbx" name="'.$name.'_cmbx"'.' size="5"' .
			' onClick="setSelMult_'.$name.'();">';
	foreach($aVal as $vX => $dX) {
		$sHtml .= '<option value="'.htmlentities($vX).'">'.htmlentities($dX).'</option>';
	}
	$sHtml.='</select>'."\n";
	$sHtml .= '<textarea class="inp1" name="'.$name.'" id="'.$name.'" cols="40" rows="5" readonly>'.
		$sVal.'</textarea>'."\n";
	$sHtml .= "<script>\n" .
			"function setSelMult_$name() {\n" .
			"   oTesto = document.getElementById('$name');\n" .
			"   oCombo = document.getElementById('".$name."_cmbx');\n" .
			"   selk = oCombo.selectedIndex;\n".
			"   tnew = oCombo.options[selk].value;\n".
			"   //alert(oTesto.value+tnew);\n".
			"   if (oTesto.value.search(tnew+';') < 0) {\n".
			"	   oTesto.value = oTesto.value+oCombo.options[selk].value+'; ';\n".
			"   } else {\n".
			"     oTesto.value = oTesto.value.replace(tnew+';','');\n".			
			"   }\n".
			"   oTesto.value = oTesto.value.replace('  ',' ');\n".			
			"}\n" .
			"</script>\n";
	return $sHtml;
}

function getHtmlHidden($name, $val='@REQ', $aOpt=array()) {
	$sVal = ($val=='@REQ')? reqVarHE($name):htmlentities($val);
	$sHtml = '<input type="hidden" name="'.$name.'" value="'.$sVal.'" style="margin:0;padding:0;">'."\n";
	return $sHtml;
}

function urlTorna($msgbck="") {
	global $objSess, $isReloadable;
	if ($isReloadable!==false) $isReloadable = true;
	$back = ($isReloadable)?-1:0;
	if ($objSess->hasHistory()) {
		return $objSess->getHistory($back)."&msgbck=".urlencode($msgbck);
	}
	return '';
}         

/*
 * restituisce l'URL della pagina precedente che contiene la stringa sPag
 */
function urlTornaPag($sPag, $msgbck="") {
	global $objSess, $isReloadable;
	if ($isReloadable!==false) $isReloadable = true;
	if ($objSess->hasHistory()) {
		return $objSess->getHistoryPag($sPag)."&msgbck=".urlencode($msgbck);
	}
	return '';
}         

function cmdTorna($testo='',$nSpazi=3) {
	global $objSess, $isReloadable, $isPopUp;
	if ($isPopUp) {
		return $testo;
	}
	$sURL = urlTorna();
	if ($sURL == "") {
		return "";
	}
	if (empty($testo)) $testo = 'torna';
	return str_repeat('&nbsp;',$nSpazi)."<a class='cmd1' href=\"".
			$sURL."\">"._t("$testo")."</a>\n";
}         

function cmdTornaPag($sPag, $testo='',$nSpazi=3) {
	global $objSess, $isReloadable, $isPopUp;
	if ($isPopUp) {
		return $testo;
	}
	$sURL = urlTornaPag($sPag);
	if ($sURL == "") {
		return "";
	}
	if (empty($testo)) $testo = 'torna';
	return str_repeat('&nbsp;',$nSpazi)."<a class='cmd1' href=\"".
			$sURL."\">"._t("$testo")."</a>\n";
}         

function backOnErr($msg) {
	global $objSess, $isReloadable;
	backPage($msg);
	exit();	
}

/**
 * Genera html per paginazione
 * $link = url pagina
 * $numRec = numero record totali
 * $startOffset (output)
 * $endOffset (output)
 */
function rglGetPaginaz($link, $numRec, &$startOffset, &$endOffset){
	global $objSess,$objDB,$sXSNQS;
	$pathPage = $_SERVER['PHP_SELF'];
	$nameScript = substr(basename($_SERVER['PHP_SELF']),0,-4);
	if ($link=='') {
		$link = $pathPage."?".$sXSNQS;
	}
	$startOffset = intval(reqVar('startoffset'));
	$recPerPag = 15;
	// legge da base_parametro il num. rec. per pagina
	if (is_object($objSess)) {
		$sSQL = " SELECT valore FROM rgl_parametri " .
				" WHERE id_utente = ".$objSess->idUtente.
				" AND sezione = 'paginaz' AND codice = '".addslashes($nameScript)."'";
		//dbg_($sSQL);
		$objDB->query($sSQL,'recxpag');
		if ($row=$objDB->fetch('recxpag')) {
			//dbg_($row);
			$recPerPag = intval($row['valore']);
		}
		if ($recPerPag < 5) $recPerPag = 5;
	}
	// se passato recperpag modifica il numero rec. per pagina e lo memorizza
	if (reqVar('recperpag')=='piu5' and $recPerPag < 100) {
		$recPerPag += 5;
		if ($recPerPag == 15) {
			$sSQL = " DELETE FROM rgl_parametri " .
					" WHERE id_utente = ".$objSess->idUtente.
					" AND sezione = 'paginaz' AND codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		} else {
			$sSQL = " REPLACE rgl_parametri SET valore = $recPerPag, " .
					" id_utente = ".$objSess->idUtente.
					" , sezione = 'paginaz' , codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		}
	}
	if (reqVar('recperpag')=='meno5' and $recPerPag > 5) {
		$recPerPag -= 5;
		if ($recPerPag == 15) {
			$sSQL = " DELETE FROM rgl_parametri " .
					" WHERE id_utente = ".$objSess->idUtente.
					" AND sezione = 'paginaz' AND codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		} else {
			$sSQL = " REPLACE rgl_parametri SET valore = $recPerPag, " .
					" id_utente = ".$objSess->idUtente.
					" , sezione = 'paginaz' , codice = '".addslashes($nameScript)."'";
			$objDB->exec($sSQL);
		}
	}
	
	if ($startOffset >= $numRec) {
		$startOffset = $numRec - $recPerPag;
	}
	if ($startOffset < 0) {
		$startOffset = 0;
	}
	$endOffset = $startOffset + $recPerPag;
	if ($endOffset > $numRec) $endOffset = $numRec;
	$lastPage = ceil($numRec / $recPerPag);
	$currPage = ceil(($startOffset+1) / $recPerPag);
	if ($currPage < 4) {
		$iStart = 1; 
	} else {
		$iStart = $currPage - 3; 
	}
	$iEnd = $iStart + 6;
	if ($iEnd > $lastPage) $iEnd = $lastPage;
	if  ($iEnd > 6) $iStart = $iEnd - 6;
	//dbg_("startOffset=$startOffset endOffset=$endOffset recPerPag=$recPerPag lastPage=$lastPage" .
	//		" currPage=$currPage iStart=$iStart iEnd=$iEnd");
	$sHTML = "";
	// aggiunge var. GET filtro
	$linkPf = $link."&pagifilt=".urlencode(reqVar('pagifilt'));
	$sHTML .= "$numRec records - "._t("pag.")." $currPage "._t("di")." $lastPage &nbsp;&nbsp;&nbsp;";
	if ($lastPage > 1) {
		if ($currPage > 1) {
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=0\" title=\""._t("prima pagina")."\">" .
					  "<img src='".RGL_URL_ROOT."/app/_img/freccia_stop_sx.gif' border='0'></a>&nbsp;";
			$offset = ($currPage - 2) * $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\""._t("pagina precedente")."\">" .
					  "<img src='".RGL_URL_ROOT."/app/_img/freccia_sx.gif' border='0'></a>&nbsp;";
		} else {
			$sHTML .= "<img src='".RGL_URL_ROOT."/app/_img/freccia_stop_sx_grigia.gif' border='0'>&nbsp;";
			$sHTML .= "<img src='".RGL_URL_ROOT."/app/_img/freccia_sx_grigia.gif' border='0'>&nbsp;";
		} 
		for($i=$iStart;$i<=$iEnd;$i++) {
			if ($i == $currPage) {
				$sHTML .= "<b>$i</b>&nbsp;";
				continue;
			}
			$offset = ($i - 1)* $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\""._t("pagina")." $i\">$i</a>&nbsp;";
		}
		if ($currPage < $lastPage) {
			$offset = $currPage * $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\""._t("pagina successiva")."\">" .
					  "<img src='".RGL_URL_ROOT."/app/_img/freccia_dx.gif' border='0'></a>&nbsp;";
			$offset = ($lastPage - 1) * $recPerPag;
			$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=$offset\" title=\""._t("ultima pagina")."\">" .
					  "<img src='".RGL_URL_ROOT."/app/_img/freccia_stop_dx.gif' border='0'></a>";
		} else {
			$sHTML .= "<img src='".RGL_URL_ROOT."/app/_img/freccia_dx_grigia.gif' border='0'>&nbsp;";
			$sHTML .= "<img src='".RGL_URL_ROOT."/app/_img/freccia_stop_dx_grigia.gif' border='0'>";
		} 
	}
	$sHTML .= "&nbsp;&nbsp;&nbsp;";
	if ($recPerPag > 5 ) {
		$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=0&recperpag=meno5\" title=\""
			._t("diminuisce a")." ".($recPerPag-5)." "._t("il numero righe per pagina")."\">"
			."<img src='".RGL_URL_ROOT."/app/_img/meno.gif' border='0'></a>&nbsp;";
	}
	if ($recPerPag < 100 ) {
		$sHTML .= "<a class='cmd1' href=\"$linkPf&startoffset=0&recperpag=piu5\" title=\""
			._t("aumenta a")." ".($recPerPag+5)." "._t("il numero righe per pagina")."\">"
			."<img src='".RGL_URL_ROOT."/app/_img/piu.gif' border='0'></a>";
	}
	$sHTML .= "&nbsp;";
	return $sHTML;
}

function rglGetFiltro($link='') {
	global $objSess,$objDB,$sXSNQS;
	$pathPage = $_SERVER['PHP_SELF'];
	if ($link=='') {
		$link = $pathPage."?".$sXSNQS;
	}
	$sHTML = "";
	//$sHTML .= "<table cellpadding=\"3\" cellspacing=\"3\" class=\"tabl01\" width=\"100%\">\n";
	//$sHTML .= "<tr>\n";
	//$sHTML .= "<td class='txt1' align='left'>";
	$sHTML .= _t("Filtro").":\n";
	$sHTML .= "<input type='text' name='pagifilt' id='pagifilt' value=\"".reqVarHE('pagifilt').
		"\" size='20' class='inp1'>&nbsp;\n";
	$sHTML .= "<a class='cmd1' href='#' onclick=\"javascript:oPgFlt=document.getElementById('pagifilt');" .
		"document.location.href='$link&pagifilt='+encodeURI(oPgFlt.value)+'&startoffset=0';\" " .
		"title=\""._t("imposta filtro")."\" style='cursor:pointer;text-decoration:underline;'>" .
		_t("imposta")."</a>&nbsp;\n";
	$sHTML .= "<a class='cmd1' href='#' onclick=\"document.location.href='$link&pagifilt=&startoffset=0';\" " .
		"title=\""._t("annulla filtro")."\" style='cursor:pointer;text-decoration:underline;'>" .
		_t("annulla")."</a>&nbsp;\n";
	//$sHTML .= "</td>\n";
	//$sHTML .= "</tr>\n";
	//$sHTML .= "</table>\n";
	return $sHTML;
}
	
function getHtmlImgGen($text, $width, $height, $coloreSfondo, $coloreTesto, $fontSize, $fontFamily='arialbd') {
	/*/////////////////// gestione ombreggiatura con css3 - non va con iexplorer
	$sHtml = "<span style='font-family:$fontFamily;font-size:".($fontSize+4).";font-weight:bold;" .
			"color=#$coloreTesto;".//background-color:$coloreSfondo;" .
			"text-shadow:#888888 2px 2px 1px;'>&nbsp; $text</span>\n";
	return $sHtml;
	//*//////////////////////////////////////////////////////////////////////////////
	$bgR =  hexdec(substr($coloreSfondo,0,2));
	$bgG =  hexdec(substr($coloreSfondo,2,2));
	$bgB =  hexdec(substr($coloreSfondo,4,2));
	$decr = 40;
	$shR = ($bgR>$decr)?$bgR - $decr:0;
	$shG = ($bgG>$decr)?$bgG - $decr:0;
	$shB = ($bgB>$decr)?$bgB - $decr:0;
	$fgR =  hexdec(substr($coloreTesto,0,2));
	$fgG =  hexdec(substr($coloreTesto,2,2));
	$fgB =  hexdec(substr($coloreTesto,4,2));
	if (!file_exists(RGL_FS_WEB_ROOT."/gimg")) {
		mkdir(RGL_FS_WEB_ROOT."/gimg",0775);
		//chmod(RGL_FS_WEB_ROOT."/gimg",0775);
	}
	$text = _t($text);
	$sMd5 = md5($text."/".$width."/".$height."/".$coloreSfondo."/".$coloreTesto."/".$fontSize."/".$fontFamily);
	$filePath = RGL_FS_WEB_ROOT."/gimg/$sMd5.png";
	$urlPath = RGL_URL_ROOT."/gimg/$sMd5.png";
	if (!file_exists($filePath) or (defined('RGL_REFRESH_IMGGEN') and RGL_REFRESH_IMGGEN)) {
		// Create the image
		$im = imagecreatetruecolor($width, $height);
		// Create some colors
		$bgCol = imagecolorallocate($im,$bgR,$bgG,$bgB);
		$shCol = imagecolorallocate($im, $shR,$shG,$shB);
		$fgCol = imagecolorallocate($im, $fgR, $fgG, $fgB);
		imagefilledrectangle($im, 0, 0, $width-1, $height-1, $bgCol);
		$font = RGL_FS_WEB_ROOT."/app/_img/".$fontFamily.".ttf";
		// crea l'ombra
		$x = 6;
		$y = $height-6;
		imagettftext($im, $fontSize, 0, $x+3, $y+3, $shCol, $font, $text);
		if (function_exists('imagefilter')) {
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
		}
		imagettftext($im, $fontSize, 0, $x, $y, $fgCol, $font, $text);
		imagepng($im,$filePath);
		//chmod($filePath,0775);
		imagedestroy($im);
	}
	$sHtml = "<img src='$urlPath' border='0'/>\n";
	return $sHtml;
}

function getHtmlImgLogo($sOpz='') {
	global $objSess, $objDB;
	$sNF = "logo50";
	$filePath = RGL_FS_WEB_ROOT."/gimg/$sNF.png";
	$urlPath = RGL_URL_ROOT."/gimg/$sNF.png";
	if (!file_exists(RGL_FS_WEB_ROOT."/gimg")) {
		mkdir(RGL_FS_WEB_ROOT."/gimg");
		//chmod(RGL_FS_WEB_ROOT."/gimg",0775);
	}
	if (!file_exists($filePath) or (defined('RGL_REFRESH_IMGGEN') and RGL_REFRESH_IMGGEN)) {
		if (file_exists(RGL_FS_WEB_ROOT.'/pers/logoAzienda50.gif')) {
			$imx = imagecreatefromgif(RGL_FS_WEB_ROOT.'/pers/logoAzienda50.gif');
		} else {
			$imx = imagecreatefromgif(RGL_FS_WEB_ROOT.'/app/_img/logoAzienda50.gif');
		}

		if ($imx === false) {
			return "???";	
		}
		
		$bgR =  255;
		$bgG =  255;
		$bgB =  255;
		$shX = round(($bgR+$bgG+$bgB)*0.25);
		if ($shX>220) $shX=220;
		$w = imagesx($imx);
		$h = imagesy($imx);
		$bo = 5; //bordo
		$sp = 4; //spostamento ombra
		$im = imagecreatetruecolor($w+10, $h+10);
		// Create some colors
		$bgCol = imagecolorallocate($im,$bgR,$bgG,$bgB);
		$shCol = imagecolorallocate($im, $shX,$shX,$shX);
		$nero = imagecolorallocate($im, 0,0,0);
		imagefilledrectangle($im, 0, 0, $w+$bo*2-1, $h+$bo*2-1, $bgCol);
		imagefilledrectangle($im, $sp, $sp, $w+$sp-2, $h+$sp-2, $shCol);
		if (function_exists('imagefilter')) {
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
			imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR);
		}
		imagecopy($im, $imx, 0,0, 0,0, $w,$h);
		imagerectangle($im, 0, 0, $w, $h, $nero);
		imagepng($im,$filePath,0);
		//chmod($filePath,0775);
		imagedestroy($imx);
		imagedestroy($im);
	}
	$sHtml = "<img src='$urlPath' $sOpz />\n";
	return $sHtml;
}
function backPage($msg) {
	global $objSess, $isReloadable;
	if ($isReloadable!==false) $isReloadable = true;
	$back = ($isReloadable)?-1:0;	
	rglRedirect($objSess->getHistory($back)."&msgbck=".urlencode($msg."<br>"));
	exit();	
}

function rglRedirect($sUrl,$msg='') {
	if (strpos($sUrl,'xsn=')===false) {
		if (strpos($sUrl,'?')===false) {
			$sUrl .= "?xsn=".XSN;
		} else {
			$sUrl .= "&xsn=".XSN;
		}
	}
	if ($msg!='') {
		$sUrl .= "&msgbck=".urlencode($msg);
	}
	if (headers_sent()) {
		echo getHtmlRedirect($sUrl);
		//dbg_("getHtmlRedirect($sUrl)");
	} else {
		header("location: ".$sUrl);
		//dbg_("header(\"location: \"$sUrl)");
	}
	exit();
}

function getHtmlRedirect($sUrl) {
	$sHtml = '';
	$sHtml .= "<script>\n";
	$sHtml .= "document.location.href=\"$sUrl\";\n";
	$sHtml .= "</script>\n";
	return $sHtml;
}
